﻿using System;
using System.Collections.Generic;

using System.Text;
using Portal.Interface;
using Portal.Environment;
using Portal.Language;
using System.Data;
using System.Data.SqlClient;

namespace Portal.CMS
{
    public class ShowListDocumentItemConfig:IWebPartConfig
    {
        public string Id
        {
            get
            {
                return "ShowListDocumentItemConfig";
            }
        }
        public string Subject 
        { 
            get
            {
                return ResourceBLL.GetText(Id, "Title", "Cấu hình webpart");
            }
        }

        public string RenderWebPart(ConfigParamCls ConfigParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(ShowListDocumentItemConfig));
            string Html = "";
           
            string WebPartId = (string)Environments.Request("WebPartId");
            string RecordId = (string)Environments.Request("RecordId");

            string OldLawDocumentId = "";
            string XmlConfigItem = Environments.GetXmlWebPartConfigItem(RecordId);
            if (XmlConfigItem != null)
            {
                try
                {
                    DataSet ds = new DataSet();
                    ds.ReadXml(new System.IO.StringReader(XmlConfigItem));
                    OldLawDocumentId = (string)ds.Tables[0].Rows[0]["LawDocumentId"];
                }
                catch { }
            }
            string SelectLawDocument = "<select id=drpSelectDocument>";
            DataTable dtLawDocuments = new LawDocumentBLL().ReadingLawDocument("");
            for (int iIndex = 0; iIndex < dtLawDocuments.Rows.Count; iIndex++)
            {
                string LawDocumentId = (string)dtLawDocuments.Rows[iIndex]["LawDocumentId"];
                string ItemSubject = (string)dtLawDocuments.Rows[iIndex]["Subject"];
                if (OldLawDocumentId.Equals(LawDocumentId))
                {
                    SelectLawDocument += "    <option selected value=\"" + LawDocumentId + "\">" + ItemSubject + "</option>";
                }
                else
                {
                    SelectLawDocument += "    <option value=\"" + LawDocumentId + "\">" + ItemSubject + "</option>";
                }
            }
            SelectLawDocument += "</select>";

            Html += 
                  "<script language=javascript>\r\n"+
                  "     function ClientSaveConfig(WebPartId,RecordId)\r\n"+
                  "     {\r\n"+
                  "         LawDocumentIndex=document.getElementById(\"drpSelectDocument\").selectedIndex;\r\n" +
                  "         LawDocumentId=document.getElementById(\"drpSelectDocument\").options[LawDocumentIndex].value;\r\n" +
                  "         rValue=Portal.CMS.ShowListDocumentItemConfig.ServerSideSaveConfig(WebPartId,RecordId,LawDocumentId).value;\r\n" +
                  "         alert(rValue);\r\n"+
                  "     }\r\n"+
                  "</script>\r\n"+
                  "<table>" +
                  " <tr>" +
                  "     <td>" + ResourceBLL.GetText(Id, "LawSubject", "Chọn vùng hiển thị") + "</td>" +
                  "     <td>" + SelectLawDocument + "</td>" +
                  " </tr>" +
                  " <tr>" +
                  "     <td></td>" +
                  "     <td><input onclick=javascript:ClientSaveConfig('"+WebPartId+"','"+RecordId+"'); type=button value=\"" + ResourceBLL.GetText(Id, "Save", "Ghi lại") + "\"><input type=button value=\"" + ResourceBLL.GetText(Id, "Cancel", "Quay lại") + "\" onclick=javascript:window.close();></td>" +
                  " </tr>" +
                  "</table>";
            return Environments.GetFormDialog(Html,Subject);
        }

        public object Reserved { get { return null; } }
        public object SetReserved(object oInput)
        {
            return null;
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideSaveConfig(
            string WebPartId,
            string RecordId,
            string LawDocumentId)
        {
            try
            {
                string XmlConfig = "";
                DataSet ds = new DataSet("XmlConfig");
                ds.Tables.Add("Config");
                ds.Tables["Config"].Columns.Add("WebPartId");
                ds.Tables["Config"].Columns.Add("RecordId");
                ds.Tables["Config"].Columns.Add("LawDocumentId");
                ds.Tables["Config"].Rows.Add(new object[]{
                    WebPartId,RecordId,LawDocumentId
                });
                XmlConfig = ds.GetXml();
                Environments.SaveWebPartItemConfig(WebPartId, RecordId, XmlConfig);
                return ResourceBLL.GetText(WebPartId, "SaveOk", "Ghi lại cấu hình thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        public void AjaxProReg(System.Web.UI.Page page)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(ShowListDocumentItemConfig), page);
        }
    }
}
